$(document).ready(function () {
  var table = $('table#table').DataTable({
    ajax: {
      url: baseUrl + '/datatable',
      data : function(d) {
              d.headerId = headerId;
		      return d;
      }
    },
    searchCols: [{
        search: headerId
      }],
    order: [
      [1, 'asc']
    ],
    columns: [{
    	data: 'headerId',
    	data: 'headerId',
    	defaultContent: '',
		visible: false
    },{
      data: 'id',
      sortable: false,
      defaultContent: '',
      'class': 'text-center',
      render: function (data, type, row, meta) {
        return '<div class="checkbox"><input type="checkbox" name="ids" value="' + data + '"/><label for="ids"/></div>'
      }
    
 
     },{
      name: 'lineNumber',
      data: 'lineNumber'
    },{
      name: 'materialCode',
      data: 'materialCode'
    },{
      name: 'materialName',
      data: 'materialName'
    },{
      name: 'qtyOrderedEach',
      data: 'qtyOrderedEach'
    },{
      name: 'qtyPalletizedEach',
      data: 'qtyPalletizedEach'
    },{
      name: 'qtyReceivedEach',
      data: 'qtyReceivedEach'
    },{
      name: 'lotattr06',
      data: 'lotattr06'
    },{
      name: 'lotattr07',
      data: 'lotattr07'
    },{
      name: 'dEdi11',
      data: 'dEdi11',
      render: function (data, type, row, meta) {
            return "XCMG";
      }
    },{
      name: 'udf07',
      data: 'udf07'
    },{
      name: 'containNumber',
      data: 'containNumber'
    },{
      name: 'lineStatus',
      data: 'lineStatus',
      render: function (data, type, row, meta) {
      	return VALUES_MAP_DICT_TYPE && VALUES_MAP_DICT_TYPE[data] ? VALUES_MAP_DICT_TYPE[data].text : '';
      }
    }, {
      data: "id",
      defaultContent: '',
      sortable: false,
      'class': 'text-center',
      render: function (data, type, row, meta) {
        return ('<div class="btn-group">' +
          '<a  class="btn btn-default btn-xs" title="修改" href="' + baseUrl + '/edit/' + row.id + '"><i class="fa fa-edit"></i></a>' + 
          '<a class="btn btn-default btn-xs del" title="删除" href="#"><i class="fa fa-trash-o"></i></a>' +
          '</div>' +
          '&nbsp;&nbsp;');
      }
    }]
  });

  table.on('click', '.del', function () {
    var id = table.row($(this).parents('tr')).data().id;
    $("#delForm #ids").val(id);
    if (id) {
      bootbox.confirm("确定要删除数据吗?", function (result) {
        if (result) {
          $("#delForm").submit();
        }
      });
    }
  });
  $(".btn.delAll").on("click", function (e) {
    var ids = [];
    $('#table>tbody input:checked').each(function (i, item) {
      ids.push($(item).val());
    });
    $("#delForm #ids").val(ids.join(","));
    if (ids.length > 0) {
      bootbox.confirm("确定要删除选中的数据吗?", function (result) {
        if (result) {
          $("#delForm").submit();
        }
      });
    } else {
      bootbox.alert("请选择要删除的数据.");
    }
  });

    $(".btn.palAll").on("click", function (e) {
        var ids = [];
        $('#table>tbody input:checked').each(function (i, item) {
            ids.push($(item).val());
        });
        $("#palletizeForm #ids").val(ids.join(","));
        if (ids.length > 0) {
            bootbox.confirm("确定要对选中的订单行进行码盘吗?", function (result) {
                if (result) {
                    $("#palletizeForm").submit();
                }
            });
        } else {
            bootbox.alert("请选择要码盘的单证行.");
        }
    });


    $(".btn.lineRecvAll").on("click", function (e) {
    var ids = [];
    $('#table>tbody input:checked').each(function (i, item) {
      ids.push($(item).val());
    });
    $("#lineRecvForm #ids").val(ids.join(","));
    if (ids.length > 0) {
        bootbox.confirm("确定要对选中的订单行进行收货吗?", function (result) {
        if (result) {
          $("#lineRecvForm").submit();
        }
      });
    } else {
      bootbox.alert("请选择要收货的单证.");
    }
  });

  
  table.on('click', '.enable', function () {
    var that = this;
    var data = table.row($(this).parents('tr')).data();
    var id = data.id;
    var enabled = data.enabled;
    bootbox.confirm("确定要更新选中的数据吗?", function(resul) {
	if (resul) {
	    $.get(baseUrl + "/enable", {
	      enabled: !enabled,
	      ids: id
	    }, function (res) {
	      if (res) {
	        data.enabled = !enabled;
	        $(that).find("i").removeClass("fa-" + (enabled ? "check-" : "") + "square-o").addClass("fa-" + (!enabled ? "check-" : "") + "square-o");
	        toastr.info('操作成功!');
	      }
	
	    });
	}});   
  });

  $('.search-form').on("submit", function (e) {
    table.draw();
    //阻止表单submit
    return false;
  });

});
